<template>
  <div class="vue-form">
    <div class="header">
      <slot name="header"></slot>
    </div>
    <el-form :label-width="labelWidth" class="form-wrap">
      <el-row>
        <template v-for="(item, _index) in formItems" :key="_index">
          <el-col v-bind="colLayout">
            <template v-if="item.type === 'input' || item.type === 'password'">
              <el-form-item
                v-if="!item.isHidden"
                :label="item.label"
                :style="itemStyle"
              >
                <el-input
                  style="width: 100%"
                  :placeholder="item.placeholder"
                  :show-password="item.password"
                  v-model="formData[`${item.field}`]"
                ></el-input>
              </el-form-item>
            </template>
            <template v-else-if="item.type === 'select'">
              <el-form-item :label="item.label" :style="itemStyle">
                <el-select
                  style="width: 100%"
                  :placeholder="item.placeholder"
                  v-model="formData[`${item.field}`]"
                >
                  <el-option
                    v-for="option in item.options"
                    :key="option.value"
                    :value="option.value"
                    >{{ option.label }}</el-option
                  >
                </el-select>
              </el-form-item>
            </template>
            <template v-else-if="item.type === 'datepicker'">
              <el-form-item :label="item.label" :style="itemStyle">
                <el-date-picker
                  style="width: 100%"
                  v-bind="item.otherOption"
                  v-model="formData[`${item.field}`]"
                ></el-date-picker>
              </el-form-item>
            </template>
          </el-col>
        </template>
      </el-row>
    </el-form>
    <div class="footer">
      <slot name="footer"></slot>
    </div>
  </div>
</template>

<script lang="ts">
import { defineComponent, PropType, ref, watch } from "vue";
import { IFormItem } from "../types/index";
export default defineComponent({
  props: {
    modelValue: {
      type: Object,
    },
    formItems: {
      type: Array as PropType<IFormItem[]>,
      default: () => [],
    },
    labelWidth: {
      type: String,
      default: "120px",
    },
    itemStyle: {
      type: Object,
      default: () => ({ padding: "0px 0px" }),
    },
    colLayout: {
      type: Object,
      default: () => ({
        xl: 6, // ≥1920px
        lg: 8, // ≥1200px
        md: 12, // ≥992px
        sm: 24, // ≥768px
        xs: 24, // <768px
      }),
    },
  },
  emit: ["update:modelValue"],
  setup(props, { emit }) {
    // 监听表单的输入，发送给父组件
    const formData = ref({ ...props.modelValue });
    watch(
      formData,
      (newValue) => {
        emit("update:modelValue", newValue);
      },
      { deep: true }
    );
    return {
      formData,
    };
  },
});
</script>

<style lang="less" scoped>
.form-wrap {
  padding-top: 22px;
}
</style>
